<template>
  <el-select v-model="selected" placeholder="请选择" @change="changeLang">
    <el-option
            v-for="option in options"
            :key="option.value"
            :label="option.text"
            :value="option.value">
    </el-option>
  </el-select>
</template>
<script>
//  (function defaultLang () {
//    let lang = window.localStorage.lang
//    if (!lang) {
//      window.localStorage.lang = 'zh_CN'
//    } else {
//      if (lang !== 'zh_CN') {
//        document.getElementById('m7LanguageStyle').href = './static/i18nStyle/lang-' + lang + '.css'
//      }
//    }
//  })()
  export default {
    name: 'i18n',
    data () {
      return {
        selected: window.localStorage.lang || 'zh_CN',
        options: [
          { text: '中文(简体)', value: 'zh_CN' },
          { text: '中文(繁體)', value: 'zh_TW' },
          { text: 'English', value: 'en' }
        ]
      }
    },
    methods: {
      changeLang () {
        window.localStorage.lang = this.selected
        this.$emit('changeLang', this.selected)
        this.$store.commit('UPDATELANG', this.selected)
        let languageStyle = document.getElementById('m7LanguageStyle')
        if (this.selected === 'zh_CN') {
          languageStyle.href = ''
        } else {
          languageStyle.href = './static/i18nStyle/lang-' + this.selected + '.css'
        }
      }
    },
    beforeMount () {
      let lang = window.localStorage.lang
      if (!lang) {
        window.localStorage.lang = 'zh_CN'
      } else {
        if (lang !== 'zh_CN') {
          document.getElementById('m7LanguageStyle').href = './static/i18nStyle/lang-' + lang + '.css'
        }
      }
    }
  }
</script>

